package android.tusnotas.model;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.tusnotas.beans.File;

public class FileModel {
	
	public long insertFile(File file, Integer folderId){
		TusNotasModel.fileInsertStmt.bindString(1, file.getName());
		TusNotasModel.fileInsertStmt.bindString(2, file.getLink());
		TusNotasModel.fileInsertStmt.bindString(3, file.getExtension());
		TusNotasModel.fileInsertStmt.bindDouble(4, file.getSize());
		TusNotasModel.fileInsertStmt.bindString(5, file.getDate());
		TusNotasModel.fileInsertStmt.bindDouble(6, folderId);
		return TusNotasModel.fileInsertStmt.executeInsert();
	}

	public List<File> selectAllFilesByFolder(Integer folderId){
		List<File> files = new ArrayList<File>();
		File file = null;
		
		String[] fields = { "id", "name", "link", "extension", "size", "date","folderId"};
		Cursor cursor = TusNotasModel.db.query(TusNotasModel.FILE_TABLE_NAME, fields,
				"folderId ='"+ folderId +"'", null, null, null, "id ASC");
		if (cursor.moveToFirst()) {
			do {
				file = new File();
				file.setId(cursor.getInt(0));
				file.setName(cursor.getString(1));
				file.setLink(cursor.getString(2));
				file.setExtension(cursor.getString(3));
				file.setSize(cursor.getLong(4));
				file.setDate(cursor.getString(5));
				file.setFolderId(folderId);
				files.add(file);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return files;
	}
}
